from kivy import Config
from kivy.app import App
from kivy.core.text import DEFAULT_FONT, LabelBase
from kivy.lang import Builder
from kivy.uix.button import Button

from ConfigComm import ConfigComm

Config.set('kivy', 'window_icon', 'pixel_cat.png')  # 设置程序图标
LabelBase.register(DEFAULT_FONT, ConfigComm.FONT_SIMKAI)

# 自定义button组件
class MyButton(Button):

    # 自定义成员变量
    label = None
    label_text = 0

    def __init__(self, **kwargs):
        super(MyButton, self).__init__(**kwargs)

    # 重写on_release方法
    def on_release(self):
        if self.label:
            self.label_text = int(self.label.text) + 1
            self.label.text = str(self.label_text)
        else:
            self.label = LabelBase(self.label_text + 1)


class MyApp(App):
    def build(self):
        self.title = '按钮'
        root = Builder.load_file('99-button.kv')
        btn = Button(text='动态添加按钮')
        btn.size_hint = (None, None)
        btn.size = (200, 50)
        # btn.bind(on_release=self.btn_release(btn, '按钮已释放'))  # 这种写法项目启动时即会执行btn_release，btn_release返回值为None，所以编译报错AssertionError: None is not callable
        btn.bind(on_release=lambda btn: self.btn_release(btn, '按钮已释放'))
        root.add_widget(btn)
        return root

    def btn_release(self, btn, msg=None):
        print(f'点击按钮[{btn.text}] msg:{msg}')



if __name__ == '__main__':
    MyApp().run()